package com.powernode.goods.service;

import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService {


    @SneakyThrows
    @Override
    public String submit(Long goodsId,Integer num) {
//        Thread.sleep (200);
//        log.info ("保存订单!");
//        //远程调用商品服务扣减库存
//        OkHttpClient client=new OkHttpClient ();
//        try {
//            Request request=new Request.Builder ().url ("http://localhost:9999/goods//decr/"+goodsId+"/"+num).build ();
//            Response response=client.newCall (request).execute ();
//        } catch (IOException e) {
//            e.printStackTrace ();
//        }

//        发一条消息给goods服务
        DefaultMQProducer defaultMQProducer = new DefaultMQProducer("producerGroup11");
        defaultMQProducer.setNamesrvAddr("192.168.100.110:9876");
        defaultMQProducer.start();
        Message message = new Message("orderTopic1",(goodsId+":"+num).getBytes(StandardCharsets.UTF_8));
        defaultMQProducer.send(message);
        defaultMQProducer.shutdown();
        return "success";
    }

}
